import OSS from 'ali-oss'
import myAxios from "@/axios/axiosConfig.js"
import requestList from "@/axios/requestList.js"
let stsInfo = {
  accessKeyId: null,
  accessKeySecret: null,
  securityToken: null,
  bucketName: null,
  dir: null,
  requestId: null,
  host: null,
  expiration:''
}
const aliurl = 'https://oss.xdeas.com.cn/'
export function put(dir, name, File) {
  try {
    return new Promise((resolve, reject) => {
      return myAxios.get(requestList.getSecurityToken,{
        params:{
          applicationName:'web'
        }
      }).then(res => {

        stsInfo = res.data
        resolve(stsInfo)
      })
      // return getOssData({
      //   applicationName:'web'
      // }).then(res => {
      //   stsInfo = res.data
      //   resolve(stsInfo)
      // })
    }).then(stsInfo => {
      const client = new OSS({
        region: 'oss-cn-beijing',
        accessKeyId: stsInfo.accessKeyId,
        accessKeySecret: stsInfo.accessKeySecret,
        bucket: stsInfo.bucketName,
        stsToken: stsInfo.token
      })
      const result = client.put(dir + name, File)
      return {
        ...result,
        url:aliurl + dir + name
      }
    })
  } catch (e) {
    console.log(e)
  }
}
